package com.voute.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.FeedCommentEntity;
import com.voute.Model.Bean.UxTagBean;
import com.voute.Model.IFeedDetailModel;
import com.voute.Model.IUxTagModel;
import com.voute.Model.Impl.FeedDetailModelImpl;
import com.voute.Model.Impl.UXTagModelImpl;
import com.voute.R;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.GlideUtils;
import com.voute.Utils.PreferenceUtils;
import com.voute.Utils.TransformUtils;
import com.voute.Utils.ViewHolderUtils;
import com.voute.View.Activity.RegisterActivity;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;
import rx.Observable;
import rx.functions.Func1;

/**
 * feed详情评论列表adapter
 * Created by liucxu on 2017/1/24.
 */

public class FeedDetailCommentAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<FeedCommentEntity> datas = new ArrayList<>();

    public FeedDetailCommentAdapter(Context c, ArrayList<FeedCommentEntity> entities) {
        if (entities != null) {
            datas.clear();
            datas.addAll(entities);
            context = c;
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup group) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_comment_linearlayout, null);
        }
        initViews(i, convertView);
        return convertView;
    }

    private void initViews(final int position, View convertView) {
        final FeedCommentEntity entity = datas.get(position);

        LinearLayout positive_layout = ViewHolderUtils.get(convertView, R.id.positive_layout);
        CircleImageView user_avaster_iv = ViewHolderUtils.get(convertView, R.id.user_avaster_iv);
        TextView user_name = ViewHolderUtils.get(convertView, R.id.user_name);
        TextView comment_ = ViewHolderUtils.get(convertView, R.id.comment_);
        final TextView positive_num = ViewHolderUtils.get(convertView, R.id.positive_num);
        TextView date = ViewHolderUtils.get(convertView, R.id.date);


        LinearLayout positive_layout_negative = ViewHolderUtils.get(convertView, R.id.positive_layout_negative);
        CircleImageView user_negative_avaster_iv = ViewHolderUtils.get(convertView, R.id.user_negative_avaster_iv);
        TextView user_negative_name = ViewHolderUtils.get(convertView, R.id.user_negative_name);
        TextView comment_negative = ViewHolderUtils.get(convertView, R.id.comment_negative);
        final TextView positive_negative_num = ViewHolderUtils.get(convertView, R.id.positive_negative_num);
        TextView date_negative = ViewHolderUtils.get(convertView, R.id.date_negative);

        if (entity.is_pos()) {
            positive_layout.setVisibility(View.VISIBLE);
            positive_layout_negative.setVisibility(View.GONE);
            GlideUtils.getInstance().LoadContextBitmap(context,entity.getAvatar(),user_avaster_iv,R.mipmap.default_avaster,R.mipmap.default_avaster,GlideUtils.LOAD_BITMAP);
            user_avaster_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Intent intent=new Intent(context, UserInfoActivity.class);
//                    intent.putExtra("key_user_id",entity.getUid()+"");
//                    context.startActivity(intent);
                }
            });
            if(TextUtils.isEmpty(entity.getUsername())){
                user_name.setText("匿名用户");
            }
            else
            {
                user_name.setText(entity.getUsername());
            }
            comment_.setText(entity.getContent());
            date.setText(entity.getAdd_date());
            if(entity.isUped()){
                Drawable drawable_negative_unselect = context.getResources().getDrawable(R.mipmap.feed_detail_hot_un_uped);
                drawable_negative_unselect.setBounds(0, 0, drawable_negative_unselect.getMinimumWidth(), drawable_negative_unselect.getMinimumHeight());//必须设置图片大小，否则不显示
                positive_num.setCompoundDrawables(null,drawable_negative_unselect,null,null);
                positive_num.setTextColor(Color.parseColor("#fe7e9d"));
                positive_num.setEnabled(false);
            }
            else{
                Drawable drawable_negative_unselect = context.getResources().getDrawable(R.mipmap.adapter_feed_vote);
                drawable_negative_unselect.setBounds(0, 0, drawable_negative_unselect.getMinimumWidth(), drawable_negative_unselect.getMinimumHeight());//必须设置图片大小，否则不显示
                positive_num.setCompoundDrawables(null,drawable_negative_unselect,null,null);
                positive_num.setTextColor(Color.parseColor("#999999"));
                positive_num.setEnabled(true);
            }
            positive_num.setText(entity.getUp() + "");
            positive_num.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    positive_num.setEnabled(false);
                    if (checkLoginStatus()) {
                        Drawable drawable_negative_unselect = context.getResources().getDrawable(R.mipmap.feed_detail_hot_un_uped);
                        drawable_negative_unselect.setBounds(0, 0, drawable_negative_unselect.getMinimumWidth(), drawable_negative_unselect.getMinimumHeight());//必须设置图片大小，否则不显示
                        positive_num.setCompoundDrawables(null,drawable_negative_unselect,null,null);
                        positive_num.setTextColor(Color.parseColor("#fe7e9d"));
                        positive_num.setEnabled(false);
                        positive_num.setText((entity.getUp()+1)+"");
                        IUxTagModel tagModel = new UXTagModelImpl();
                        final IFeedDetailModel model=new FeedDetailModelImpl();
                        tagModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<BaseBean>>() {
                            @Override
                            public Observable<BaseBean> call(UxTagBean uxTagBean) {
                                return model.upComment(entity.getCid()+"",uxTagBean.getData().getUxtag(),1);
                            }
                        }).compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
                            @Override
                            public void onSuccess(BaseBean response) {

                            }

                            @Override
                            public void onFailure(BaseBean response) {

                            }
                        });
                    }
                }
            });
        } else {
            positive_layout.setVisibility(View.GONE);
            positive_layout_negative.setVisibility(View.VISIBLE);
            GlideUtils.getInstance().LoadContextBitmap(context,entity.getAvatar(),user_negative_avaster_iv,R.mipmap.default_avaster,R.mipmap.default_avaster,GlideUtils.LOAD_BITMAP);
            user_negative_avaster_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Intent intent=new Intent(context, UserInfoActivity.class);
//                    intent.putExtra("key_user_id",entity.getUid()+"");
//                    context.startActivity(intent);
                }
            });
            if(TextUtils.isEmpty(entity.getUsername())){
                user_negative_name.setText("匿名用户");
            }
            else{
                user_negative_name.setText(entity.getUsername());
            }
            if(entity.isUped()){
                Drawable drawable_negative_unselect = context.getResources().getDrawable(R.mipmap.feed_detail_hot_un_uped);
                drawable_negative_unselect.setBounds(0, 0, drawable_negative_unselect.getMinimumWidth(), drawable_negative_unselect.getMinimumHeight());//必须设置图片大小，否则不显示
                positive_negative_num.setCompoundDrawables(null,drawable_negative_unselect,null,null);
                positive_negative_num.setTextColor(Color.parseColor("#fe7e9d"));
                positive_negative_num.setEnabled(false);
            }
            else{
                Drawable drawable_negative_unselect = context.getResources().getDrawable(R.mipmap.adapter_feed_vote);
                drawable_negative_unselect.setBounds(0, 0, drawable_negative_unselect.getMinimumWidth(), drawable_negative_unselect.getMinimumHeight());//必须设置图片大小，否则不显示
                positive_negative_num.setCompoundDrawables(null,drawable_negative_unselect,null,null);
                positive_negative_num.setTextColor(Color.parseColor("#999999"));
                positive_negative_num.setEnabled(true);
            }
            comment_negative.setText(entity.getContent());
            positive_negative_num.setText(entity.getUp() + "");
            date_negative.setText(entity.getAdd_date());
            positive_negative_num.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    positive_negative_num.setEnabled(false);
//                    UpCommentAction action=new UpCommentAction();
//                    action.setSide(0);
//                    action.setCid(entity.getCid());
//                    RxBus.getDefault().post(action);
                    if (checkLoginStatus()) {
                        Drawable drawable_negative_unselect = context.getResources().getDrawable(R.mipmap.feed_detail_hot_un_uped);
                        drawable_negative_unselect.setBounds(0, 0, drawable_negative_unselect.getMinimumWidth(), drawable_negative_unselect.getMinimumHeight());//必须设置图片大小，否则不显示
                        positive_negative_num.setCompoundDrawables(null,drawable_negative_unselect,null,null);
                        positive_negative_num.setTextColor(Color.parseColor("#fe7e9d"));
                        positive_negative_num.setEnabled(false);
                        positive_negative_num.setText((entity.getUp()+1)+"");
                        IUxTagModel tagModel = new UXTagModelImpl();
                        final IFeedDetailModel model=new FeedDetailModelImpl();
                        tagModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<BaseBean>>() {
                            @Override
                            public Observable<BaseBean> call(UxTagBean uxTagBean) {
                                return model.upComment(entity.getCid()+"",uxTagBean.getData().getUxtag(),0);
                            }
                        }).compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
                            @Override
                            public void onSuccess(BaseBean response) {

                            }

                            @Override
                            public void onFailure(BaseBean response) {

                            }
                        });
                    }
                }
            });
        }
    }

    private void separateComments() {
    }

    public void setDatas(ArrayList<FeedCommentEntity> values) {
        if (values != null) {
            datas.clear();
            datas.addAll(values);
            separateComments();
            notifyDataSetChanged();
        }
    }

    public boolean checkLoginStatus(){
        if(TextUtils.isEmpty(PreferenceUtils.GetUserToken())){
            Intent intent=new Intent(context, RegisterActivity.class);
            context.startActivity(intent);
            return false;
        }
        return true;
    }
}
